package com.woniuxy.classteacher.service.impl;

import com.woniuxy.classteacher.mapper.ChangeClassMapper;
import com.woniuxy.classteacher.mapper.ClassMapper;
import com.woniuxy.classteacher.mapper.StudentMapper;
import com.woniuxy.classteacher.service.StudentDataAnalyzeService;
import com.woniuxy.commons.entity.wyh.TeamInfo;
import com.woniuxy.commons.exception.NotClassMasterException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: 牟强
 * @Date: 2020/7/30/030 11:06
 * @Description: 学生数据分析
 */
@Service
public class StudentDataAnalyzeServiceImpl implements StudentDataAnalyzeService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ChangeClassMapper changeClassMapper;
    @Autowired
    private ClassMapper classMapper;

    /**
     * @Author: 牟强
     * @Date: 2020/7/30/030 15:49
     * @Description； 初始化学生年级排名分析
     * @Param: [request]
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> initStudentRankingChart(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        //从request中获取refreshtoken
        String refreshtoken = request.getHeader("refreshToken");
        //通过token获取教师id
        Map<Object, Object> data = redisTemplate.opsForHash().entries(refreshtoken);
        //获取教师id
        int rid = (Integer) data.get("rid");
        //先查询出当前登录教师的班主任id
        List<Integer> tmIds = changeClassMapper.findTmIdByTeacherId(rid);
        //判断是否是班主任
        if (tmIds.size() < 1) {
            throw new NotClassMasterException("不是班主任，无法录入学生信息");
        }
        //通过班主任id批量查询班级信息
        List<TeamInfo> teamInfos = classMapper.findTeamInfoByTmId(tmIds);
        //获取所有考试名称
        List<String> paperNames = studentMapper.selectTestPaperName();
        map.put("teamInfos", teamInfos);
        map.put("paperNames", paperNames);
        return map;
    }

    /**
     * @Author: 牟强
     * @Date: 2020/7/30/030 15:45
     * @Description: 学生年级排名分析
     * @Param: [subjectId, teamId, gradeName, request]
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     */
    @Override
    public Map<String, Object> studentRankingChart(int teamId, String gradeName, HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        System.out.println(gradeName);
        int ten = 0;
        int twenty = 0;
        int thirty = 0;
        int forty = 0;
        int fifty = 0;
        int sixty = 0;
        int seventy = 0;
        int eighty = 0;
        int ninety = 0;
        int hundred = 0;
        //总分
        //先查出10%需要查几条数据
        Integer integer = studentMapper.selectStudentNum(gradeName);
        //查出在0%~10%的学生的Id
        List<Integer> stuIds1 = studentMapper.selectScopeStudentByNum(gradeName, (1 - 1) * integer, integer);
        //查出在10%~20%的学生的Id
        List<Integer> stuIds2 = studentMapper.selectScopeStudentByNum(gradeName, (2 - 1) * integer, integer);
        //查出在20%~30%的学生的Id
        List<Integer> stuIds3 = studentMapper.selectScopeStudentByNum(gradeName, (3 - 1) * integer, integer);
        //查出在30%~40%的学生的Id
        List<Integer> stuIds4 = studentMapper.selectScopeStudentByNum(gradeName, (4 - 1) * integer, integer);
        //查出在40%~50%的学生的Id
        List<Integer> stuIds5 = studentMapper.selectScopeStudentByNum(gradeName, (5 - 1) * integer, integer);
        //查出在50%~60%的学生的Id
        List<Integer> stuIds6 = studentMapper.selectScopeStudentByNum(gradeName, (6 - 1) * integer, integer);
        //查出在60%~70%的学生的Id
        List<Integer> stuIds7 = studentMapper.selectScopeStudentByNum(gradeName, (7 - 1) * integer, integer);
        //查出在70%~80%的学生的Id
        List<Integer> stuIds8 = studentMapper.selectScopeStudentByNum(gradeName, (8 - 1) * integer, integer);
        //查出在80%~90%的学生的Id
        List<Integer> stuIds9 = studentMapper.selectScopeStudentByNum(gradeName, (9 - 1) * integer, integer);
        //查出在90%~100%的学生的Id
        List<Integer> stuIds10 = studentMapper.selectScopeStudentByNum(gradeName, (10 - 1) * integer, integer);
        //判断在0%~10%这群学生中是否有本班的学生
        System.out.println(stuIds1.size());
        System.out.println(stuIds1);
        for (int i = 0; i < stuIds1.size(); i++) {
            //查询该学生班级id
            if (stuIds1.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds1.get(i));
                if (teamId == studentClassId) {
                    ten++;
                }
            }
        }
        //判断在10%~20%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds2.size(); i++) {
            //查询该学生班级id
            if (stuIds2.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds2.get(i));
                if (teamId == studentClassId) {
                    twenty++;
                }
            }
        }
        //判断在20%~30%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds3.size(); i++) {
            //查询该学生班级id
            if (stuIds3.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds3.get(i));
                if (teamId == studentClassId) {
                    thirty++;
                }
            }
        }
        //判断在30%~40%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds4.size(); i++) {
            //查询该学生班级id
            if (stuIds4.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds4.get(i));
                if (teamId == studentClassId) {
                    forty++;
                }
            }
        }
        //判断在40%~50%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds5.size(); i++) {
            //查询该学生班级id
            if (stuIds5.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds5.get(i));
                if (teamId == studentClassId) {
                    fifty++;
                }
            }
        }
        //判断在50%~60%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds6.size(); i++) {
            //查询该学生班级id
            if (stuIds6.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds6.get(i));
                if (teamId == studentClassId) {
                    sixty++;
                }
            }
        }
        //判断在60%~70%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds7.size(); i++) {
            //查询该学生班级id
            if (stuIds7.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds7.get(i));
                if (teamId == studentClassId) {
                    seventy++;
                }
            }
        }
        //判断在70%~80%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds8.size(); i++) {
            //查询该学生班级id
            if (stuIds8.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds8.get(i));
                if (teamId == studentClassId) {
                    eighty++;
                }
            }
        }
        //判断在80%~90%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds9.size(); i++) {
            //查询该学生班级id
            if (stuIds9.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds9.get(i));
                if (teamId == studentClassId) {
                    ninety++;
                }
            }
        }
        //判断在90%~100%这群学生中是否有本班的学生
        for (int i = 0; i < stuIds10.size(); i++) {
            //查询该学生班级id
            if (stuIds10.get(i) != null) {
                Integer studentClassId = studentMapper.findStudentClassIdByStuId(stuIds10.get(i));
                if (teamId == studentClassId) {
                    hundred++;
                }
            }
        }
        map.put("ten", ten);
        map.put("twenty", twenty);
        map.put("thirty", thirty);
        map.put("forty", forty);
        map.put("fifty", fifty);
        map.put("sixty", sixty);
        map.put("seventy", seventy);
        map.put("eighty", eighty);
        map.put("ninety", ninety);
        map.put("hundred", hundred);
        return map;
    }
}
